//
//  MyAttentionViewController.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/3/15.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "MyAttentionViewController.h"
#import "AttentionTableViewCell.h"
#import "CustomProductTableViewCell.h"
#import "CollectionModel.h"
#import "ProductListsTableViewCell.h"
@interface MyAttentionViewController ()<UITableViewDataSource,UITableViewDelegate>{
    CollectionModel *collectionModel;
    NSMutableArray *dataArray;
}
@property(nonatomic,strong)CollectionModel *collectionModel;
@end

@implementation MyAttentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNaviTitle:@"关注"];
    [self setNaviLeftButton:@""];
    [self loadData];
    // Do any additional setup after loading the view.
}
-(void)loadData{
    __weak typeof(self) weakSelf = self;
     [[LKDHud shareHud] showLoadingView];
    [ApiClient getCollectionListPage:@"1" didFinishLoaded:^(CollectionModel *model, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        if ([model.status intValue]==1) {
            weakSelf.collectionModel=model;
            [weakSelf formatData];
            if (![self.view.subviews containsObject:self.displayView]) {
                [self.view addSubview:self.displayView];
            }else{
                [self.displayView reloadData];
            }
            
        }
       
    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
}
-(void)formatData{
    if (!dataArray) {
        dataArray=[[NSMutableArray alloc] init];
    }
    [dataArray removeAllObjects];
    if (_collectionModel.data.products.count==0) {
        [dataArray addObject:@"temp"]; //没有收藏的产品是插入一个空值
        [dataArray addObject:@"otherLove"];
        [dataArray addObject:_collectionModel.data.recommend];
        
        
    }else{
        [dataArray addObject:_collectionModel.data.products];
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UITableView*)displayView{
    if (!_displayView) {
        _displayView=[[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];
        _displayView.delegate=self;
        _displayView.dataSource=self;
        _displayView.backgroundColor=[UIColor clearColor];
        _displayView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _displayView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];
        // [_displayView setSeparatorColor:[UIColor redColor]];
    }
    return _displayView;
}
#pragma mark-UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_collectionModel.data.products.count==0) {
        id obj=dataArray[indexPath.row];
        if ([obj isKindOfClass:[NSString class]]) {
            if ([obj isEqualToString:@"temp"]) {
                QLKEmptyCellTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:kEmptyCellTableViewCellIdentify];
                if (!cell) {
                    cell = [[QLKEmptyCellTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kEmptyCellTableViewCellIdentify];
                }
                [cell setLogoImage: @"sign_guanzhu" cellHeight:self.displayView.height];
                cell.titleLabel.text = @"还没有关注的宝贝" ;
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
                return cell;
 
            }else{
                static NSString *identifier = @"AttentionTableViewCell";
                AttentionTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:
                                                identifier];
                if (cell == nil) {
                    cell = [[[NSBundle mainBundle] loadNibNamed:@"AttentionTableViewCell"  owner:self options:nil] lastObject];
                    
                }
                cell.selectionStyle=UITableViewCellSelectionStyleNone;
                [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
                return cell;

            }
            
            
        }else{
            ProductListsTableViewCell *cell=[ProductListsTableViewCell cellWithTableView:tableView];
            [cell setCollectionModel:_collectionModel.data];
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
            return cell;

        }
        
        
    }else{
        ProductListsTableViewCell *cell=[ProductListsTableViewCell cellWithTableView:tableView];
        [cell setCollectionModel:_collectionModel.data];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        return cell;

    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_collectionModel.data.products.count==0) {
        id obj=dataArray[indexPath.row];
        if ([obj isKindOfClass:[NSString class]]) {
            if ([obj isEqualToString:@"temp"]) {
              return 280;
            }else{
             return 20;
                
            }
            
        }else{
            CollectionSubModel *productsModel=_collectionModel.data;
            int count=productsModel.recommend.count;
            
            int lineNum=0;
            if (count%2==0) {
                lineNum=count/2;
            }else{
                lineNum=count/2+1;
            }
            
            return lineNum*240;
        }
        
        
    }else{
        CollectionSubModel *productsModel=_collectionModel.data;
        int count=productsModel.products.count;
        
        int lineNum=0;
        if (count%2==0) {
            lineNum=count/2;
        }else{
            lineNum=count/2+1;
        }
        
        return lineNum*240;
        
    }

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
